package com.ruoyi.parking.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.parking.service.IParkCarService;
import com.ruoyi.util.AliyunOcrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;
@RestController
@RequestMapping("/parking/licensePlate")
public class IdentificationController extends BaseController {
    @Autowired
    private AliyunOcrUtil aliyunOcrUtil;
    @Autowired
    private IParkCarService parkCarService;
    @Value("${recognition.path}")
    private String path;
    @PostMapping("/uploadIdNumberImg")
    public AjaxResult uploadCarNumberImg(MultipartFile imgFile) throws Exception {
        //System.out.println("==================24"+imgFile);
        if(imgFile.isEmpty()){
            return AjaxResult.error("上传的文件为空!");
        }
        //保存的路径
        // String path = "E:\\webspace\\图片";

        String filePath =  path+"/" +imgFile.getOriginalFilename();
        //
        imgFile.transferTo(new File(filePath));
        //调用阿里云OCR
        Map<String, Object> result = aliyunOcrUtil.IdentificationNumberPlate(filePath);
        if (result==null){
            return AjaxResult.error("识别失败");
        }
        result.put("filePath",filePath);

        return AjaxResult.success(result);
    }

    /**
     * 根据识别的车牌查找carId是否存在
     * @param licensePlate
     * @return
     */
    @RequestMapping("/selectCarId")
    public AjaxResult selectCarId(@RequestParam String licensePlate){
        int carId = parkCarService.selectCarIdByLicensePlate(licensePlate);

        return AjaxResult.success("查找成功").put("carId",carId);
    }
}
